package com.cwt.admin.system;

import cn.dev33.satoken.stp.StpUtil;
import com.cwt.admin.common.BaseController;
import com.cwt.common.result.AjaxResult;
import com.cwt.service.service.SysRoleMenuService;
import com.cwt.service.service.SysUserService;
import com.cwt.service.vo.Login;
import com.cwt.service.vo.Register;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author chenwt
 * @version V1.0
 * @describe 【登录控制】
 * @date 2023年07月19日 14:27:27
 */
@Api(tags = "登录注册", value = "LoginController")
@RequestMapping()
@RestController
public class LoginController extends BaseController {

    @Autowired
    private SysUserService userService;

    @Autowired
    private SysRoleMenuService roleMenuService;

    @ApiOperation(value = "登录", protocols = "http", httpMethod = "POST")
    @PostMapping("/login")
    public AjaxResult login(@Valid @RequestBody Login login) {
        return userService.login(login, "admin"); // admin: 123456
    }

    @ApiOperation(value = "注册", protocols = "http", httpMethod = "POST")
    @PostMapping("/register")
    public AjaxResult register(@Valid @RequestBody Register register) {
        return userService.adminRegister(register);
    }

    @ApiOperation(value = "退出", protocols = "http", httpMethod = "POST")
    @PostMapping("/logout")
    public AjaxResult logout(@RequestBody Long userId) {
        StpUtil.logout(userId);
        return AjaxResult.success("退出成功");
    }

    @ApiOperation(value = "登录状态", protocols = "http", httpMethod = "POST")
    @PostMapping("/isLogin")
    public AjaxResult isLogin() {
        return AjaxResult.success(StpUtil.isLogin());
    }

    @ApiOperation(value = "获取菜单列表", protocols = "http", httpMethod = "POST")
    @PostMapping("/getMenuList")
    public AjaxResult getMenuList() {
        return roleMenuService.selectMenusByRoleId(getRoleId());
    }

}
